package com.eliza.music.model.vm

import android.os.CountDownTimer
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import javax.inject.Inject


class VmSplash @Inject constructor() : ViewModel() {
    private var _sleepDuration = MutableStateFlow(0L)
    val sleepDuration: StateFlow<Long> = _sleepDuration
    private var downTimer: CountDownTimer? = null

    private var _nextToMain = MutableStateFlow(false)
    val nextToMain: StateFlow<Boolean> = _nextToMain

    init {
        delayToNext()
    }

    private fun delayToNext(time: Long = 3000) {
        downTimer = object : CountDownTimer(time, 1000) {
            override fun onTick(p0: Long) {
                _sleepDuration.value = p0 / 1000 + 1
            }

            override fun onFinish() {
                toNext()
            }
        }.start()
    }

    private fun toNext() {
        _nextToMain.value = true
    }

    fun onClick() {
        downTimer?.cancel()
        toNext()
    }

}
